package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: RoleController
 * @Author: zhouyangyang
 * @Date: 2020/2/17 10:03
 * @Description:
 */

/**
 * 角色
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    /**
     *分页查询
     *@returns
     */
    @RequestMapping("/findByCondition")
    public Result findByCondition(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = roleService.findByCondition(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FALL);
        }
    }
    /**
     *新增角色
     *@returns
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleService.add(role,permissionIds);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FALL);
        }

    }
    /**
     *根据id查找角色
     *@returns
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FALL);
        }
    }
    /**
     *根据roleId获取权限id
     *@returns
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            Integer[] ids = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FALL);
        }
    }
    /**
     *编辑角色
     *@returns
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] ids){
        try {
            roleService.edit(role,ids);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FALL);
        }
    }
    /**
     *删除角色
     *@returns
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            roleService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FALL);
        }
    }
}
